package com.policyCloud.policyOauth.service.impl;

import com.policyCloud.policyOauth.dao.CenterRoleMapper;
import com.policyCloud.policyOauth.dao.CenterRoleSitemapMapper;
import com.policyCloud.policyOauth.dao.CenterSitemapMapper;
import com.policyCloud.policyOauth.dao.CenterUsersRoleMapper;
import com.policyCloud.policyOauth.pojo.*;
import com.policyCloud.policyOauth.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Resource
    CenterUsersRoleMapper usrRoleMapper;
    @Resource
    CenterRoleMapper roleMapper;
    @Resource
    CenterRoleSitemapMapper roleResourceMapper;
    @Resource
    CenterSitemapMapper resourceMapper;


    @Override
    public Map<String, Object> getRoleAndPer(CenterUsers user) {

        Map<String, Object> reMap = new HashMap<>();

        Integer uid = user.getId();
        CenterUsersRoleCriteria URcriteriaObj = new CenterUsersRoleCriteria();
        CenterUsersRoleCriteria.Criteria criteria= URcriteriaObj.createCriteria();
        criteria.andUserIdEqualTo(uid);
        int re = (int) usrRoleMapper.countByExample(URcriteriaObj);
        if(re>0){
            List<CenterUsersRole> rIdByUid = usrRoleMapper.selectByExample(URcriteriaObj);
            List<Integer> rIds = new LinkedList<>();
            rIdByUid.forEach(x ->{
                rIds.add(x.getRoleId());
            });
            CenterRoleCriteria roleCriteria = new CenterRoleCriteria();
            CenterRoleCriteria.Criteria rCriteria= roleCriteria.createCriteria();
            rCriteria.andIdIn(rIds);
            List<CenterRole> roles = roleMapper.selectByExample(roleCriteria);
            reMap.put("roles",roles);
            CenterRoleSitemapCriteria roleResourceCriteria = new CenterRoleSitemapCriteria();
            CenterRoleSitemapCriteria.Criteria rrCriteria = roleResourceCriteria.createCriteria();
            rrCriteria.andRoleIdIn(rIds);
            int re2= (int) roleResourceMapper.countByExample(roleResourceCriteria);
            if(re2>0){
                List<CenterRoleSitemap> resourceByrole =roleResourceMapper.selectByExample(roleResourceCriteria);
                List<String> resourceIds = new LinkedList<>();
                resourceByrole.forEach(x ->{
                    resourceIds.add(x.getAppId());
                });

                CenterSitemapCriteria resourceCriteria = new CenterSitemapCriteria();
                CenterSitemapCriteria.Criteria resourceCriteriaCriteria= resourceCriteria.createCriteria();
                resourceCriteriaCriteria.andAppIdIn(resourceIds);
                int re3 = (int) resourceMapper.countByExample(resourceCriteria);
                if(re3>0){
                    List<CenterSitemap> perms = resourceMapper.selectByExample(resourceCriteria);
                    reMap.put("perms",perms);
                }

            }

        }
        return reMap;
    }



}
